﻿
using System.Collections;
using UnityEngine;
using YooAsset;

namespace Framework.Resource
{

    public partial class YooassetResourceHelper
    {
        private string packageVersion;
        

        // IEnumerator UpdateResources(UpdateResourceVersionCallback updateResourcesVersionCompleteCallback)
        // {
        //     yield return  StartCoroutine(UpdatePackageVersion(updateResourcesVersionCompleteCallback));
        // }
        private IEnumerator UpdatePackageVersion(UpdateResourceVersionCallback updateResourcesVersionCompleteCallback)
        {
            if (Mode == EPlayMode.OfflinePlayMode)
            {
                updateResourcesVersionCompleteCallback.Invoke(true,false);
                yield break;
            }
            var package = YooAssets.GetPackage(packageName);
            string oldPackageVersion = package.GetPackageVersion();
            var operation = package.UpdatePackageVersionAsync();
            yield return operation;

            if (operation.Status == EOperationStatus.Succeed)
            {
                //更新成功
                packageVersion = operation.PackageVersion;
                Debug.Log($"Updated package Version : {packageVersion}");
                if (oldPackageVersion != packageVersion)
                {
                    updateResourcesVersionCompleteCallback.Invoke(true,true);
                    // yield return StartCoroutine(UpdatePackageManifest(updateResourcesVersionCompleteCallback));
                }
                else
                {
                    updateResourcesVersionCompleteCallback.Invoke(true,false);
                }
               
            }
            else
            {
                updateResourcesVersionCompleteCallback.Invoke(false,false);
                //更新失败
                Debug.LogError(operation.Error);
            }
        }
        
        private IEnumerator UpdatePackageManifest(DownloadCallback downloadCallback)
        {
            Debug.Log($"UpdatePackageManifest");
            // 更新成功后自动保存版本号，作为下次初始化的版本。
            // 也可以通过operation.SavePackageVersion()方法保存。
            bool savePackageVersion = true;
            var package = YooAssets.GetPackage(packageName);
            
            
            var operation = package.UpdatePackageManifestAsync(packageVersion, savePackageVersion);
            yield return operation;

            if (operation.Status == EOperationStatus.Succeed)
            {

                yield return StartCoroutine(Download(downloadCallback));
                // downloadCallback.OnDownloadOverFunction(true);
            }
            else
            {
                downloadCallback.OnDownloadErrorFunction.Invoke("manifest",operation.Error);
                // // updateResourcesVersionCompleteCallback.Invoke(false,true);
                // //更新失败
                // Debug.LogError(operation.Error);
            }
        }
        
        IEnumerator Download(DownloadCallback downloadCallback)
        {
            
            
            int downloadingMaxNum = 10;
            int failedTryAgain = 3;
            var package = YooAssets.GetPackage(packageName);
            package.ForceUnloadAllAssets();
            ///必须提前下载文件tag
            string[] tags = { "must_down" };
            var downloader = package.CreateResourceDownloader(tags,downloadingMaxNum, failedTryAgain);
    
            //没有需要下载的资源
            if (downloader.TotalDownloadCount == 0)
            {
                downloadCallback.OnDownloadOverFunction(true);
                yield break;
            }

            //需要下载的文件总数和总大小
            int totalDownloadCount = downloader.TotalDownloadCount;
            long totalDownloadBytes = downloader.TotalDownloadBytes;    
            
            //注册回调方法
            downloader.OnDownloadErrorCallback = (fileName, error) =>
            {
                downloadCallback.OnDownloadErrorFunction(fileName, error);
            };
            downloader.OnDownloadProgressCallback =(totalDownloadCount,currentDownloadCount,totalDownloadBytes,currentDownloadBytes)=>
            {
                downloadCallback.OnDownloadProgressUpdateFunction(totalDownloadCount,currentDownloadCount,totalDownloadBytes,currentDownloadBytes);
            };
            downloader.OnDownloadOverCallback =(isSucceed)=>
            {
                downloadCallback.OnDownloadOverFunction(isSucceed);
            };
            downloader.OnStartDownloadFileCallback = (fileName,sizeBytes)=>
            {
                downloadCallback.OnStartDownloadFileFunction(fileName,sizeBytes);
            };

            //开启下载
            downloader.BeginDownload();
            yield return downloader;

            //检测下载结果
            if (downloader.Status == EOperationStatus.Succeed)
            {
                //下载成功
            }
            else
            {
                //下载失败
            }
        }
    }

}